package leetcode.easy;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 2. 问题：多线程顺序打印1-100，线程1打印123，线程2打印456，线程3打印789... 以此类推，不能出现乱序。
 * @author TANGYE
 * @date 2021/2/24 9:46 下午
 **/
public class Two {
    private static Lock lock = new ReentrantLock();

    private final static int MAX = 100;
    private static int num = 1;
    private static int loopNum = 3;

    public static class PrintNum{
        private Condition cd1;
        private Condition cd2;
        private Condition cd3;

        public PrintNum(Condition cd1, Condition cd2, Condition cd3) {
            this.cd1 = cd1;
            this.cd2 = cd2;
            this.cd3 = cd3;
        }

        public void first() {
            try {
                lock.lock();
                while (num < MAX) {
                    if (loopNum == 0) {
                        loopNum = 3;
                        cd1.await();
                    }
                    if (num > MAX) {
                        return;
                    }
                    System.out.println(Thread.currentThread().getName()+": "+num);
                    num++;
                    loopNum--;
                    if (loopNum == 0){
                        cd2.signal();
                    }
                }
                cd2.signal();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }

        public void second() {
            try {
                lock.lock();
                while (num < MAX) {
                    if (loopNum == 0) {
                        loopNum = 3;
                        cd2.await();
                    }
                    if (num > MAX) {
                        return;
                    }
                    System.out.println(Thread.currentThread().getName()+": "+num);
                    num++;
                    loopNum--;
                    if (loopNum == 0){
                        cd3.signal();
                    }
                }
                cd3.signal();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }

        public void third() {
            try {
                lock.lock();
                while (num < MAX) {
                    if (loopNum == 0) {
                        loopNum = 3;
                        cd3.await();
                    }
                    if (num > MAX) {
                        return;
                    }
                    System.out.println(Thread.currentThread().getName()+": "+num);
                    num++;
                    loopNum--;
                    if (loopNum == 0){
                        cd1.signal();
                    }
                }
                cd1.signal();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
    }

    public static void main(String[] args) {
        PrintNum printNum = new PrintNum(lock.newCondition(), lock.newCondition(), lock.newCondition());
        Thread t1 = new Thread(() -> printNum.first(),"Thread1");
        Thread t2 = new Thread(() -> printNum.second(),"Thread2");
        Thread t3 = new Thread(() -> printNum.third(),"Thread3");

        t1.start();
        t2.start();
        t3.start();
    }


}
